$(function() {

	new Vue({
		el: "#addGoodApp",
		data: {
			fullscreenLoading: false,
			districtDefault: [],
			fileData: new FormData(),
			goodId: "",
			fileAvatarList:[],
			carouseImgUrl: [],
			detailImgUrl: [],
			propImgUrl:[],
			goodPropList: [],
			uploadUrl: "http://39.100.131.147:8888/goods/address/uploadFile",
			index: 1,
			goodPropTableData: [],
			value: "请选择商品分类",
			goodPropFrom: {
				price: 1,
				inventory: 9999,
				photo: "",
				commodityId: "",
				combination: "",
			},
			uploadForm: {
				categoryId: "",
			},
			form: {
				name: "",
				goodName: "",
				newPrice: "",
				oldPrice: "",
				inventory: 99999,
				warnInventory: 1,
				unit: "个",
			},
			options: [],
		},
		created: function() {
			parent.vueData.$data.hreHtml = "../addGood/index.html";
			parent.vueData.activeIndex = "7";
			parent.vueData.getLeftNav("7")
			parent.vueData.$data.categoryIndex = 1
			this.getAllCategory()
		},
		methods: {
			getAllCategory: function() {
				this.fullscreenLoading = true;
				let shopId = getShopId();
				axios({
					url: POSTURL.selectAllGoodCategory,
					method: 'post',
					data: {
						shopId: shopId,
						admin_grade:"1",
					}
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						for (let i = 0; i < response.data.result.length; i++) {
							let obj = response.data.result[i]
							let category = new Object();
							category.value = obj.name + "+" + obj.uid;
							category.label = obj.name;
							category.children = [];
							for (let j = 0; j < obj.secondary.length; j++) {
								let data = obj.secondary[j]
								let secondCategory = new Object();
								secondCategory.value = data.name + "+" + data.uid
								secondCategory.label = data.name
								category.children.push(secondCategory)
							}
							this.options.push(category)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//选择分类
			handleChange(data) {
				let s = []
				for (let i = 0; i < data.length; i++) {
					let obj = data[i];
					s.push(obj.split("+")[0])
					if (i === 1) {
						this.uploadForm.categoryId = obj.split("+")[1]

					}
				}
				this.value = s.join('>')
				this.form.name = this.value
				this.getGoodProp(this.uploadForm.categoryId)
			},
			lastStep() {
				if (this.index > 1) {
					this.index--;
				}
			},
			nextStep() {
				if (this.index == 2) {
					if (stringEmpty(this.goodId)) {
						this.$message.error("请先发布商品");
					} else {
						this.index++;
					}
				} else {
					this.index++;
				}
			},
			handleCoverSuccess(response, file, fileList) {
				if (response.code == 100) {
					
					this.form.cover = response.result
					this.fileAvatarList.push({
						name: "商品封面",
						url: response.result,
					})
				}
			},
			beforeAvaterRemove(file,fileList) {
				  let index = this.fileAvatarList.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.fileAvatarList.splice(index, 1)
				return true;
			},
			beforeCourseRemove(file, fileList) {
				  let index = this.carouseImgUrl.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.carouseImgUrl.splice(index, 1)
				return true;
			},
			beforeDetailRemove(file, fileList) {
				  let index = this.detailImgUrl.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				  this.detailImgUrl.splice(index, 1)
				return true;
			},
			beforePropRemove(file, fileList) {
				  let index = this.propImgUrl.findIndex( fileItem => {
				       return fileItem.url === file.url
				   })
				 this.propImgUrl.splice(index, 1)
				return true;
			},
			handleCarouselSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.carouseImgUrl.push({
						name: "商品轮播图",
  			            url: response.result,
					})
					
				}
			},
			handleDetailSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.detailImgUrl.push({
						name: "商品详情图",
					    url: response.result,
					})
					
				}
			},
			handleCategoryCoverSuccess(response, file, fileList) {
				if (response.code == 100) {
					this.goodPropFrom.photo = response.result
					this.propImgUrl.push({
						name: "商品规格图",
					    url: response.result,
					})
				}
			},
			beforeAvatarUpload(file) {
				// 文件类型进行判断  
				const isJPG = file.type === "image/png";
				const isLt2M = file.size / 1024 / 1024 < 2;

				if (!isJPG) {
					this.$message.error("上传头像图片只能是 JPG 格式!");
				}
				if (!isLt2M) {
					this.$message.error("上传头像图片大小不能超过 2MB!");
				}
				return isJPG && isLt2M;
			},
			handleCoverExceed(files, fileList) {
				this.$message.warning("商品封面只能上传一张图片")
			},
			handleCarouseExceed(files, fileList) {
				this.$message.warning("商品轮播图最多上传3张")
			},
			handleDetailExceed(files, fileList) {
				this.$message.warning("商品详情图最多上传3张")
			},
			handleCategoryCoverExceed(files, fileList) {
				this.$message.warning("商品规格封面最多上传1张")
			},
			//添加商品规格
			getSelectGoodCategory() {
				if (!this.isNotNumber(this.goodPropFrom.price)) {
					this.$message.error('商品规格售价只能为数字');
					return;
				}
				if (!this.isNotNumber(this.goodPropFrom.inventory)) {
					this.$message.error('商品规格库存只能为数字');
					return;
				}
				let combination = "";

				let combinationArry = [];
				for (let i = 0; i < this.goodPropList.length; i++) {
					let value = this.goodPropList[i];
					if (!stringEmpty(value.select)) {
						combinationArry.push(value.select)
					}
				}
				combination = combinationArry.join('+')
				if (combinationArry.length <= 0) {
					this.$message.error('请选择规格参数');
					return;
				}
				if (this.propImgUrl.length<=0) {
					this.$message.error('商品规格图不能为空');
					return;
				}
				this.goodPropFrom.commodityId = this.goodId
				let uploadGoodProp = {}
				uploadGoodProp.commodityId = this.goodPropFrom.commodityId
				uploadGoodProp.price = regYuanToFen(this.goodPropFrom.price, 100)
				uploadGoodProp.inventory = this.goodPropFrom.inventory
				uploadGoodProp.photo = this.goodPropFrom.photo
				uploadGoodProp.combination = combination
				uploadGoodProp.admin_grade ="1"
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.addGoodParameterGroup,
					method: 'post',
					data: uploadGoodProp
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						this.propImgUrl= [];
						this.goodPropFrom.price = "";
						for (let i = 0; i < this.goodPropList.length; i++) {
							let value = this.goodPropList[i];
							value.select = ""
						}
						this.getAllCategoryByCommodityId()
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})

			},
			// 删除商品规格
			deleteGoodPropRow(val) {
				let index = this.goodPropTableData.indexOf(val);
				if (index > -1) {
					this.goodPropTableData.splice(index, 1);
				}
				axios({
					url: POSTURL.deleteGoodPropById,
					method: 'post',
					data: {
						id: val.uid,
						admin_grade:"1",
					}
				}).then(response => {
					if (response.data.code === 100) {
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {

					console.log(error);
					this.$message.error('服务器出错!');
				})
			},

			//根据商品查询所有参数组合
			getAllCategoryByCommodityId() {
				axios({
					url: POSTURL.selectByCommodityId,
					method: 'post',
					data: {
						commodityId: this.goodId,
						admin_grade:"1",
					}
				}).then(response => {
					if (response.data.code === 100) {
						this.goodPropTableData = response.data.result
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {

					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			//获取商品规格列表
			getGoodProp(categoryId) {
				let shopId = getShopId();
				let param = {
					shopId: shopId,
					categoryId: categoryId,
					admin_grade:"1",
				}
				axios({
					url: POSTURL.getCategoryList,
					method: 'post',
					data: param
				}).then(response => {
					if (response.data.code === 100) {
						for (let i = 0; i < response.data.result.length; i++) {
							let obj = response.data.result[i]
							let category = new Object();
							category.name = obj.name;
							category.select = "";
							category.options = [];
							for (let j = 0; j < obj.optionals.length; j++) {
								let data = obj.optionals[j]
								let secondCategory = new Object();
								secondCategory.name = data;

								category.options.push(secondCategory)
							}
							this.goodPropList.push(category)
						}
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {

					console.log(error);
					this.$message.error('服务器出错!');
				})
			},
			isNotNumber(inputData) {
				if (parseFloat(inputData).toString() == "NaN") {
					return false;
				} else {
					return true;
				}
			},
			//发布商品
			releaseGood() {

				if (stringEmpty(this.form.goodName)) {
					this.$message.error('商品名不能为空');
					return;
				}
				if (!this.isNotNumber(this.form.newPrice)) {
					this.$message.error('商品售价只能为数字');
					return;
				}
				if (!this.isNotNumber(this.form.oldPrice)) {
					this.$message.error('成本价只能为数字');
					return;
				}
				if (stringEmpty(this.form.inventory)) {
					this.$message.error('库存不能为空');
					return;
				}
				if (stringEmpty(this.form.warnInventory)) {
					this.$message.error('库存警告数量不能为空');
					return;
				}
				if (stringEmpty(this.form.unit)) {
					this.$message.error('商品单位不能为空');
					return;
				}
				if (this.fileAvatarList.length<=0) {
					this.$message.error('商品封面不能为空');
					return;
				}
				if (this.carouseImgUrl.length <= 0) {
					this.$message.error('商品轮播图不能为空');
					return;
				}
				if (this.detailImgUrl.length <= 0) {
					this.$message.error('商品详情图不能为空');
					return;
				}
				
				this.uploadForm.name = this.form.goodName
				this.uploadForm.newPrice = regYuanToFen(this.form.newPrice, 100)
				this.uploadForm.oldPrice = regYuanToFen(this.form.oldPrice, 100)
				this.uploadForm.inventory = this.form.inventory
				this.uploadForm.inventoryWarning = this.form.warnInventory
				this.uploadForm.unit = this.form.unit
				this.uploadForm.title = this.form.goodName
				this.uploadForm.categoryId = this.districtDefault[1].split("+")[1]
				let detailsList = [];
				this.detailImgUrl.forEach(obj=>{
					detailsList.push(obj.url);
				})
				this.uploadForm.detail = detailsList.join(',')
				let carList = [];
				this.carouseImgUrl.forEach(obj=>{
					carList.push(obj.url);
				})
				this.uploadForm.photo =carList.join(',')
				this.uploadForm.cover  = this.form.cover
				this.uploadForm.shopId = getShopId()
				this.uploadForm.admin_grade = "1"
				console.log(this.uploadForm);
				this.fullscreenLoading = true;
				axios({
					url: POSTURL.insertCommodity,
					method: 'post',
					data: this.uploadForm
				}).then(response => {
					this.fullscreenLoading = false;
					if (response.data.code === 100) {
						this.goodId = response.data.result;
						this.$message({
							showClose: true,
							message: response.data.message,
							type: 'success'
						});
						
						this.form = {
							goodName: "",
							newPrice: "",
							oldPrice: "",
							inventory: 99999,
							warnInventory: 1,
							unit: "个",
						}
						this.form.name = this.value
						this.detailImgUrl= []
						this.carouseImgUrl = []
						this.form.cover = ""
						this.nextStep()
						
						
						
					} else {
						this.$message.error(response.data.message);
					}
				}).catch(error => {
					this.fullscreenLoading = false;
					console.log(error);
					this.$message.error('服务器出错!');
				})
			}
		},
	})
})
